package com.hezhi.trafficcopsys.service.impl;

import java.util.List;

import com.hezhi.framework.exception.APPException;
import com.hezhi.framework.service.impl.BaseServiceImpl;
import com.hezhi.trafficcopsys.domain.User;
import com.hezhi.trafficcopsys.persistence.dao.UserDao;
import com.hezhi.trafficcopsys.persistence.dao.impl.UserDaoImpl;
import com.hezhi.trafficcopsys.service.UserService;

public class UserServiceImpl extends BaseServiceImpl implements UserService{
	
	private UserDao userDao = null;
	
	public UserServiceImpl(){
		userDao = new UserDaoImpl();
	}
	
	@Override
	public User doLogin(User user) throws APPException{
		User dbUser = userDao.getUser(user.getUsername());
		if( dbUser == null){
			throw new APPException("用户：" + user.getUsername() + "不存在！");
		}
		if (!user.getPassword().equals(dbUser.getPassword())) {
			dbUser = null;
			throw new APPException("密码不正确！请重新输入。");
		}
		return dbUser;
	}

	@Override
	public User findUser(String username) {
		return userDao.getUser(username);
	}
	
	@Override
	public List<User> listUserByPage(int pageSize, int pageNo){
		return userDao.listUserByPage( pageSize, pageNo);
	}

	@Override
	public boolean isExistedUser(String username) throws APPException{
		boolean isExisted = false;
		if( userDao.getUser(username) != null ){
			isExisted = true;
		} else {
			throw new APPException("用户：" + username + "不存在！");
		}
		return isExisted;
	}

	@Override
	public int addUser(User user) {
		return userDao.addUser(user);
	}

	@Override
	public int delUsers(String[] username ) {
		return userDao.delUser(username);
	}
	@Override
	public int editUser(User user) {
		return userDao.editUser(user);
	}
	
	@Override
	public int getTotalSize() {
		return userDao.getTotalSize();
	}
}
